package com.yjd.pub.credit.service;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.credit.model.CreditLineApplyModel;
import com.yjd.comm.credit.model.CreditLineDataModel;
import com.yjd.comm.credit.service.ICreditLineApplyService;
import com.yjd.comm.credit.service.ICreditLineDataService;
import com.yjd.comm.enums.CreditDataRefTableEnum;
import com.yjd.comm.enums.RecordIncr;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.JpushUtil;
import com.yjd.comm.vo.Constant;
import com.yjd.pub.credit.dao.ICreditLineApplyDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：信用额度申请业务实现<br>
 * <b>time</b>：2017-06-26 13:47:36 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("creditLineApplyService")
public class CreditLineApplyServiceImpl extends BaseServiceImpl implements ICreditLineApplyService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public CreditLineApplyModel insert(CreditLineApplyModel model) throws Exception {
		return this.creditLineApplyDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(CreditLineApplyModel model) throws Exception {
		return this.creditLineApplyDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.creditLineApplyDao.deleteModel(paramMap, true);
	}

	@Override
	public int apply(Long id, Integer status) throws Exception {
		CreditLineApplyModel clam = creditLineApplyDao.getModelById(id, false);
		if(clam==null){
			FrameUtil.throwBaseException1("您审核记录不存在");
		}
		if(creditLineApplyDao.updateByMap(FrameUtil.newHashMap("id", id, "version", clam.getVersion(), "status", status), true)!=1){
			FrameUtil.throwBaseException1("审核失败！");
		}
		if(1 == status){
			UserDataModel userData = userDataService.getModelById(clam.getUser_id(), DbWREnums.READ);
			// 申请的信用额度小于 当前用户额度 为非法情况
			if(clam.getApply_credit_line() < userData.getCredit_line()){
				FrameUtil.throwBaseException1("参数错误，请核对后重试。");
			}
			Long currTime = FrameUtil.getTime(null);
			UserDataModel updateData = new UserDataModel();
			updateData.setUser_id(userData.getUser_id());
			updateData.setWhere_version(userData.getVersion());
			updateData.setLast_update_time(currTime);
			updateData.setCredit_line(clam.getApply_credit_line());
			Double changeCredit = ArithUtil.sub(clam.getApply_credit_line(),userData.getCredit_line());
			Double canCredit = ArithUtil.add(userData.getResidual_amount(),changeCredit);
			updateData.setResidual_amount(canCredit);
			int count = userDataService.update(updateData);
			if(count !=1 ){
				FrameUtil.throwBaseException1("系统繁忙，稍后重试。");
			}
			//记录用户信用额度使用明细
			CreditLineDataModel creditLineDataModel = new CreditLineDataModel();
			creditLineDataModel.setUser_id(userData.getUser_id());
			creditLineDataModel.setCost(ArithUtil.abs(changeCredit));
			creditLineDataModel.setType(changeCredit>0?RecordIncr.addAm.getCode():RecordIncr.reduceAm.getCode());
			creditLineDataModel.setResidual_amount(canCredit);
			creditLineDataModel.setRef_id(clam.getId());
			creditLineDataModel.setRef_type(CreditDataRefTableEnum.mfq_credit_line_apply.getValue());
			creditLineDataModel.setCreate_time(currTime);
			creditLineDataService.insert(creditLineDataModel);

			JpushUtil.pushMessage(
					clam.getUser_id(),
					Constant.SysParamKeyEnum.PUSH_TPL_CREDIT_LINE_CHANGED,
					clam.getApply_credit_line(),
					canCredit);
		}
		return 1;
	}

	@Resource
	private IUserDataService userDataService;

	@Resource
	private ICreditLineApplyDao creditLineApplyDao;

	@Resource
	private ICreditLineDataService creditLineDataService;

	@Override
	public IBaseDao getBaseDao() {
		return this.creditLineApplyDao;
	}

}
